package com.zzedu.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzAgentGrade;
import com.zzedu.domain.ZzAgent;
import com.zzedu.dto.ZzAgentDTO;
import com.zzedu.service.IZzAgentGradeService;
import com.zzedu.service.IZzAgentService;
import com.zzedu.vo.ZzAgentVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 代理商管理Controller
 *
 * @author Mr.Wang
 * @date 2025-01-25
 */
@RestController
@RequestMapping("/agent/agent")
public class ZzAgentController extends BaseController {
    @Autowired
    IZzAgentService zzAgentService;

    @Autowired
    IZzAgentGradeService agentGradeService;

    /**
     * 查询代理商管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzAgentDTO zzAgent) {
        IPage<ZzAgent> page = zzAgentService.page(zzAgent.getPageDTO(), zzAgentService.buildQuery(zzAgent));
        return getDataTable(page, zzAgentService.convertVO(page.getRecords()));
    }

    /**
     * 导出代理商管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:export')")
    @Log(title = "代理商管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(HttpServletResponse response, ZzAgentDTO zzAgent) {
        List<ZzAgent> list = zzAgentService.list(zzAgentService.buildQuery(zzAgent));
        ExcelUtil<ZzAgentVO> util = new ExcelUtil<ZzAgentVO>(ZzAgentVO.class);
        util.exportExcel(response, zzAgentService.convertVO(list), "代理商管理数据");
    }


    /**
     * 新增保存代理商管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:add')")
    @Log(title = "代理商管理", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@RequestBody ZzAgentDTO zzAgentDTO) {
        ZzAgent zzAgent = new ZzAgent();
        BeanUtils.copyBeanProp(zzAgent, zzAgentDTO);
        String[] provinceAndCityAndDistrict = zzAgentDTO.getProvinceAndCityAndDistrict();
        if (null != provinceAndCityAndDistrict) {
            zzAgent.setProvince(provinceAndCityAndDistrict[0]);
            if (provinceAndCityAndDistrict.length > 1) {
                zzAgent.setCity(provinceAndCityAndDistrict[1]);
            }
            if (provinceAndCityAndDistrict.length > 2) {
                zzAgent.setDistrict(provinceAndCityAndDistrict[2]);
            }
        }
        return toAjax(zzAgentService.save(zzAgent));
    }


    /**
     * 获取代理商管理详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ZzAgent zzAgent = zzAgentService.getById(id);
        List<String> provinceAndCityAndDistrict = Lists.newArrayList(zzAgent.getProvince(), zzAgent.getCity(), zzAgent.getDistrict());
        ZzAgentGrade zzAgentGrade = agentGradeService.getById(zzAgent.getGradeId());
        JSONObject jsonObject = (JSONObject) JSON.toJSON(zzAgent);
        jsonObject.put("provinceAndCityAndDistrict", provinceAndCityAndDistrict);
        jsonObject.put("gradeName","");
        if(null!= zzAgentGrade){
            jsonObject.put("gradeName", zzAgentGrade.getGradeName());
        }
        jsonObject.remove("deleted");
        jsonObject.remove("gmtCreate");
        jsonObject.remove("gmtModify");
        jsonObject.remove("createBy");
        jsonObject.remove("updateBy");
        return success(jsonObject);
    }


    /**
     * 修改保存代理商管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:edit')")
    @Log(title = "代理商管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@RequestBody ZzAgentDTO zzAgentDTO) {
        ZzAgent zzAgent = new ZzAgent();
        BeanUtils.copyBeanProp(zzAgent, zzAgentDTO);
        String[] provinceAndCityAndDistrict = zzAgentDTO.getProvinceAndCityAndDistrict();
        if (null != provinceAndCityAndDistrict) {
            zzAgent.setProvince(provinceAndCityAndDistrict[0]);
            if (provinceAndCityAndDistrict.length > 1) {
                zzAgent.setCity(provinceAndCityAndDistrict[1]);
            }
            if (provinceAndCityAndDistrict.length > 2) {
                zzAgent.setDistrict(provinceAndCityAndDistrict[2]);
            }
        }
        return toAjax(zzAgentService.updateById(zzAgent));
    }

    /**
     * 删除代理商管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('agent:agent:remove')")
    @Log(title = "代理商管理", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzAgentService.removeBatchByIds(ids));
    }
}
